#ifndef PDTD_HPP
#define PDTD_HPP

#include "ap_int.h"
#include "ap_fixed.h"
#include "hls_stream.h"
#include "fpal.hpp" // 包含定点数运算逻辑库
#include "hr.hpp"   // 包含 HR 模块的头文件，获取 pixel_fragment_packet_fixed 定义

// -----------------------------------------------------------
// PDTD与DDR_Write_Engine之间传输的写操作指令包
// -----------------------------------------------------------
struct ddr_write_packet_fixed {
    fpal::fp_screen_pixel_idx_t screen_x;       // 屏幕X坐标
    fpal::fp_screen_pixel_idx_t screen_y;       // 屏幕Y坐标
    fpal::fp_z_buffer_t new_z;                  // 新的插值深度值
    fpal::fp_color_channel_t pure_color_R;      // 纯色R
    fpal::fp_color_channel_t pure_color_G;      // 纯色G
    fpal::fp_color_channel_t pure_color_B;      // 纯色B
};

// -----------------------------------------------------------
// DDR AXI Master 接口使用的 64 位数据类型
// -----------------------------------------------------------
// 一个 64 位字可以存储 4 个 16 位的深度值
typedef ap_uint<64> zbuffer_ddr_word_t;
// 一个 64 位字可以存储 2 个 24 位 RGB 颜色值 (打包为 32 位)
typedef ap_uint<64> color_ddr_word_t;

// ===========================================================
// DDR_Write_Engine 模块的内部 DATAFLOW 辅助结构体
// ===========================================================

// Stream of requests for Z-buffer read operations
struct z_rmw_request_packet { // Renamed to reflect RMW nature for the cache
    ap_uint<32> z_word_idx;
    ap_uint<3>  z_sub_word_offset;
    ddr_write_packet_fixed write_op; // Pass the original write_op for later stages
};

// 包含所有必要的FB写入信息，不区分读写，直接指示写入
struct fb_write_request_packet {
    ap_uint<32> color_word_idx;
    ap_uint<1>  color_sub_word_offset;
    fpal::fp_color_channel_t pure_color_R;
    fpal::fp_color_channel_t pure_color_G;
    fpal::fp_color_channel_t pure_color_B;
};
struct ddr_op_count_packet {
    bool is_valid_write; // true if a write happened
};


// -----------------------------------------------------------
// PDTD (Pixel Depth Test & Dispatch) 模块的主HLS函数声明
// -----------------------------------------------------------
void pixel_depth_test_and_dispatch(
    hls::stream<pixel_fragment_packet_fixed>& input_pixel_fragment_stream,
    hls::stream<ddr_write_packet_fixed>& output_ddr_write_stream,
    const ap_uint<11> max_x_res,
    const ap_uint<11> max_y_res,
    hls::stream<ap_uint<32>>& input_fragments_count_stream,           // 从HR接收的像素片段总数通过流传输
    hls::stream<ap_uint<32>>& output_valid_fragments_count_stream     // 输出有效像素计数流给DWE
);

// -----------------------------------------------------------
// DDR_Write_Engine 子模块声明 (Updated)
// -----------------------------------------------------------

// Sub-process 1: Calculate addresses and issue Z-buffer RMW request
void dwe_calc_addr_and_z_rmw_req(
    hls::stream<ddr_write_packet_fixed>& input_ddr_write_stream,
    hls::stream<ap_uint<32>>& input_valid_fragments_count_stream,
    hls::stream<z_rmw_request_packet>& output_z_rmw_req_stream,
    hls::stream<ap_uint<32>>& output_z_rmw_req_count_stream, // Output request count for the next stage
    const ap_uint<11> max_x_res
);

// Sub-process 2: Z-Buffer Write-Combining Cache (New module for robust RMW)
void dwe_z_buffer_write_cache(
    hls::stream<z_rmw_request_packet>& input_z_rmw_req_stream,
    hls::stream<ap_uint<32>>& input_z_rmw_req_count_stream,
    hls::stream<fb_write_request_packet>& output_fb_write_req_stream,
    hls::stream<ap_uint<32>>& output_fb_write_req_count_stream,
    hls::stream<ddr_op_count_packet>& output_actual_z_write_stream, // Each packet is a successful Z-write to DDR
    hls::stream<ap_uint<32>>& output_actual_z_write_count_stream,
    zbuffer_ddr_word_t* output_zbuffer_ddr_read,   // M_AXI port for Z-buffer read
    zbuffer_ddr_word_t* output_zbuffer_ddr_write, // M_AXI port for Z-buffer write
    const ap_uint<11> max_x_res
);

// Sub-process 3: Framebuffer Read-Modify-Write
void dwe_fb_rmw(
    hls::stream<fb_write_request_packet>& input_fb_write_req_stream,
    hls::stream<ap_uint<32>>& input_fb_write_req_count_stream,
    color_ddr_word_t* output_framebuffer_ddr // M_AXI port
);

// Sub-process 4: Aggregate write operations count
void dwe_count_aggregator(
    hls::stream<ddr_op_count_packet>& input_actual_z_write_stream, // Each packet signifies a successful Z-write
    hls::stream<ap_uint<32>>& input_actual_z_write_count_stream, // Total successful Z-writes count
    hls::stream<ap_uint<32>>& output_final_count_stream // Output final count
);

// -----------------------------------------------------------
// DDR_Write_Engine 模块的主HLS函数声明
// -----------------------------------------------------------
void ddr_write_engine(
    hls::stream<ddr_write_packet_fixed>& input_ddr_write_stream,
    hls::stream<ap_uint<32>>& input_valid_fragments_count_stream, // 从PDTD接收的有效片段总数
    const ap_uint<11> max_x_res,
    const ap_uint<11> max_y_res,
    zbuffer_ddr_word_t* output_zbuffer_ddr_read,   // 指向深度缓冲区的 64 位 AXI Master (只读) (修正类型)
    zbuffer_ddr_word_t* output_zbuffer_ddr_write, // 指向深度缓冲区的 64 位 AXI Master (只写) (修正类型)
    color_ddr_word_t* output_framebuffer_ddr,         // 指向帧缓冲区的 64 位 AXI Master
    ap_uint<32>& output_actual_write_ops              // 输出参数，报告实际写入DDR的像素数
);

#endif // PDTD_HPP
